home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / Talking Steve / Source / CBasicApp.cp next >
Encoding:
Text File  |  2001-06-23  |  5.8 KB  |  273 lines

  1. // ===========================================================================
  2. //    CBasicApp.cp                 ©1994-2000 Metrowerks Inc. All rights reserved.
  3. // ===========================================================================
  4. //    This file contains the starter code for a basic PowerPlant project
  5.  
  6. #include "CBasicApp.h"
  7.  
  8. #include <LGrowZone.h>
  9. #include <PP_Messages.h>
  10. #include <PP_Resources.h>
  11. #include <UDrawingState.h>
  12. #include <UMemoryMgr.h>
  13. #include <URegistrar.h>
  14.  
  15. #include <LWindow.h>
  16. #include <LCaption.h>
  17. #include <LPicture.h>
  18.  
  19. #include <AERegistry.h>
  20.  
  21. #include <SpeechSynthesis.h>
  22.  
  23.     // Constant declarations
  24. const ResIDT    PPob_SampleWindow            = 128;
  25.  
  26. static int abs(int X)
  27. {
  28.     return (X<0) ? -X : X;
  29. }
  30.  
  31. // ===========================================================================
  32. //    • main
  33. // ===========================================================================
  34.  
  35. int main()
  36. {                            
  37.         // Set Debugging options
  38.     SetDebugThrow_(debugAction_Alert);
  39.     SetDebugSignal_(debugAction_Alert);
  40.  
  41.         // Initialize Memory Manager. Parameter is the number of
  42.         // master pointer blocks to allocate
  43.     InitializeHeap(3);
  44.     
  45.         // Initialize standard Toolbox managers
  46.     UQDGlobals::InitializeToolbox();
  47.     
  48.         // Install a GrowZone to catch low-memory situations    
  49.     new LGrowZone(20000);
  50.  
  51.         // Create the application object and run
  52.     CBasicApp    theApp;
  53.     theApp.Run();
  54.     
  55.     return 0;
  56. }
  57.  
  58.  
  59. // ---------------------------------------------------------------------------
  60. //    • CBasicApp                                        [public]
  61. // ---------------------------------------------------------------------------
  62. //    Application object constructor
  63.  
  64. CBasicApp::CBasicApp()
  65. {
  66.     unsigned long secs;
  67.     RegisterClasses();
  68.     
  69.     theWindow = NULL;
  70.     activeTime = 0;
  71.     
  72.     GetDateTime(&secs);
  73.     SetQDGlobalsRandomSeed(secs);
  74.     
  75.     StartIdling();
  76. }
  77.  
  78.  
  79. // ---------------------------------------------------------------------------
  80. //    • ~CBasicApp                                    [public, virtual]
  81. // ---------------------------------------------------------------------------
  82. //    Application object destructor
  83.  
  84. CBasicApp::~CBasicApp()
  85. {
  86.     // Nothing
  87. }
  88.  
  89.  
  90. // ---------------------------------------------------------------------------
  91. //    • StartUp                                        [protected, virtual]
  92. // ---------------------------------------------------------------------------
  93. //    Perform an action in response to the Open Application AppleEvent.
  94. //    Here, issue the New command to open a window.
  95.  
  96. void
  97. CBasicApp::StartUp()
  98. {
  99.     SetActivateTime();
  100.     ShowMyWindow();
  101. }
  102.  
  103. const int N_STRINGS = 6;
  104.  
  105. void
  106. CBasicApp::GetSaying(StringPtr str)
  107. {
  108.     GetIndString(str,128,(abs(Random())%N_STRINGS)+1);
  109. }
  110.  
  111. void
  112. CBasicApp::ShowMyWindow()
  113. {
  114.     if (theWindow == NULL)
  115.     {
  116.         Str255 str;
  117.         ProcessSerialNumber psn;
  118.  
  119.         theWindow = LWindow::CreateWindow(PPob_SampleWindow, this);
  120.         ThrowIfNil_(theWindow);
  121.  
  122.         GetSaying(str);
  123.         theWindow->FindPaneByID(111)->SetDescriptor(str);
  124.  
  125.         GetCurrentProcess(&psn);
  126.         SetFrontProcess(&psn);
  127.  
  128.         theWindow->Show();
  129.         
  130.         while (SpeechBusy()) {/* wait for any speech to complete */}
  131.         SpeakString(str);
  132.         
  133.     }
  134. }
  135.  
  136. void
  137. CBasicApp::DeleteMyWindow()
  138. {
  139.     if (theWindow)
  140.         delete theWindow;
  141.     theWindow = NULL;
  142. }
  143.  
  144. // ---------------------------------------------------------------------------
  145. //    • ObeyCommand                                    [public, virtual]
  146. // ---------------------------------------------------------------------------
  147. //    Respond to Commands. Returns true if the Command was handled, false if not.
  148.  
  149. Boolean
  150. CBasicApp::ObeyCommand(
  151.     CommandT    inCommand,
  152.     void*        ioParam)
  153. {
  154.     Boolean        cmdHandled = true;    // Assume we'll handle the command
  155.  
  156.     switch (inCommand) {
  157.  
  158.         case cmd_New: {
  159.             break;
  160.         }
  161.  
  162.         default: {
  163.             cmdHandled = LApplication::ObeyCommand(inCommand, ioParam);
  164.             break;
  165.         }
  166.     }
  167.     
  168.     return cmdHandled;
  169. }
  170.  
  171.  
  172. // ---------------------------------------------------------------------------
  173. //    • FindCommandStatus                                [public, virtual]
  174. // ---------------------------------------------------------------------------
  175. //    Determine the status of a Command for the purposes of menu updating.
  176.  
  177. void
  178. CBasicApp::FindCommandStatus(
  179.     CommandT    inCommand,
  180.     Boolean&    outEnabled,
  181.     Boolean&    outUsesMark,
  182.     UInt16&        outMark,
  183.     Str255        outName)
  184. {
  185.     switch (inCommand) {
  186.  
  187.         case cmd_New: {
  188.             break;
  189.         }
  190.  
  191.         default: {
  192.             LApplication::FindCommandStatus(inCommand, outEnabled,
  193.                                             outUsesMark, outMark, outName);
  194.             break;
  195.         }
  196.     }
  197. }
  198.  
  199.  
  200. // ---------------------------------------------------------------------------
  201. //    • RegisterClasses                                [protected]
  202. // ---------------------------------------------------------------------------
  203. //    To reduce clutter within the Application object's constructor, class
  204. //    registrations appear here in this seperate function for ease of use.
  205.  
  206. void
  207. CBasicApp::RegisterClasses()
  208. {
  209.     RegisterClass_(LWindow);
  210.     RegisterClass_(LCaption);
  211.     RegisterClass_(LPicture);
  212. }
  213.  
  214. void
  215. CBasicApp::SpendTime(const EventRecord&    /*inMacEvent */)
  216. {
  217.     if (TickCount() >= activeTime)
  218.         {
  219.         if (theWindow == NULL)
  220.             {
  221.             ShowMyWindow();
  222.             activeTime = TickCount() + 300;
  223.             if ((Random()%5) == 1)
  224.                 SteveIsUpset();
  225.             }
  226.         else
  227.             {
  228.             DeleteMyWindow();
  229.             SetActivateTime();
  230.             }
  231.         }
  232. }
  233.  
  234. void
  235. CBasicApp::SetActivateTime()
  236. {
  237.     activeTime = TickCount() + 10 + (abs(Random())%35)*60;
  238. }
  239.  
  240. void
  241. CBasicApp::EventSuspend(const EventRecord& inMacEvent)
  242. {
  243.     if (theWindow != NULL)
  244.         {
  245.         StopIdling();
  246.         DeleteMyWindow();
  247.         SetActivateTime();
  248.         StartIdling();
  249.         }
  250.     LApplication::EventSuspend(inMacEvent);
  251. }
  252.  
  253. void
  254. CBasicApp::SteveIsUpset()
  255. {
  256.  
  257.     AEDesc    finderAddr;
  258.     AppleEvent mySleep, reply;
  259.     OSErr err;
  260.     
  261.     while (SpeechBusy()) {/* if steve is speaking, wait for him to finish */}
  262.  
  263.     err = AECreateDesc((unsigned long)typeApplSignature, "MACS", 4L, &finderAddr);
  264.     err = AECreateAppleEvent('fndr', 'slep', &finderAddr, kAutoGenerateReturnID,
  265.             kAnyTransactionID, &mySleep);
  266.     err = AESend(&mySleep, &reply, kAENoReply + kAECanSwitchLayer + kAEAlwaysInteract,
  267.             kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
  268.     AEDisposeDesc(&finderAddr);
  269.     AEDisposeDesc(&mySleep);
  270.     AEDisposeDesc(&reply);
  271.  
  272. }
  273.